import {
  getInfoGroupByType,
  selectConfigurationTrainType,
} from "../request/system";
import { useSystemrStore } from "../stores/system";

export const getUrlParam = (paraName: string) => {
  var url = document.location.toString();
  var arrObj = url.split("?");

  if (arrObj.length > 1) {
    var arrPara = arrObj[1].split("&");
    var arr;

    for (var i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");

      if (arr != null && arr[0] === paraName) {
        return arr[1];
      }
    }
    return "";
  } else {
    return "";
  }
};

export function isInnerNet(ip: string) {
  console.log("地址栏输入的ip或域名：" + ip);
  if (ip == "localhost" || ip == "127.0.0.1") return true; //内网
  var segment = ip.split(".");
  if (segment[0] == "10") {
    return true; //内网
  } else if (segment[0] == "172") {
    var s2 = parseInt(segment[1]);
    if (s2 >= 16 && s2 <= 31) {
      return true; //内网
    } else return false;
  } else if (segment[0] == "192" && segment[1] == "168") {
    return true;
  } else return false; //外网
}
export function getConfigurationTrainType(lineNo: string, trainType: string) {
  const gpsKey = lineNo + "_" + trainType;
  const systemStore = useSystemrStore();
  //如果还没存数据
  if (
    Object.keys(JSON.parse(sessionStorage.getItem("selectedMap") as string))
      .length == 0
  ) {
    getInfoGroupByType().then((res: any) => {
      let data = res.bodyData,
        gk;
      for (let key in data) {
        gk = key.substring(0, 2).startsWith("0")
          ? key.substring(1, 2) + "_" + key
          : key.substring(0, 2) + "_" + key;
        systemStore.selectedMap[gk] = true;
        systemStore.lineGpsMap[gk] = data[key].lineGpsMap;
        systemStore.station[gk] = data[key].station;
      }
    });
    return;
  }
  if (systemStore.selectedMap[gpsKey]) {
    return;
  }
  console.log("getConfigurationTrainType trainType", trainType);
  selectConfigurationTrainType({ lineNo, trainType }).then((data: any) => {
    if (data && typeof data == "object") {
      const dataInfo = data.bodyData;
      if (data.code == 200 && Object.keys(dataInfo).length) {
        const tmpLineGpsMap = dataInfo["lineGpsMap"];
        const tmpStation = dataInfo["station"];
        if (
          tmpStation[gpsKey].length &&
          tmpStation[gpsKey][0]["pathType"] == 1
        ) {
          changeGpsAndStation(gpsKey, tmpLineGpsMap, tmpStation);
        }
        if (
          Object.keys(tmpLineGpsMap).length &&
          Object.keys(tmpStation).length
        ) {
          systemStore.lineGpsMap[gpsKey] = tmpLineGpsMap[gpsKey];
          systemStore.station[gpsKey] = tmpStation[gpsKey];
        }
      }
      systemStore.selectedMap[gpsKey] = true;
    }
  });
}
function changeGpsAndStation(gpsKey: string, lineGpsMap: any, station: any) {
  const lineGpsTmp = lineGpsMap[gpsKey];
  const lineGpsTmpChild = lineGpsTmp[0];
  const stationTmp = station[gpsKey];
  if (Object.keys(lineGpsTmp).length) {
    const gpsFirstTmp = lineGpsTmpChild[0];
    const gpslastTmp = lineGpsTmpChild[lineGpsTmpChild.length - 1];
    let gpsEditTmp: any = {};
    for (let index in gpsFirstTmp) {
      gpsEditTmp[index] = gpsFirstTmp[index];
    }
    gpsEditTmp.sequence = gpslastTmp.sequence + 1;
    lineGpsTmpChild.push(gpsEditTmp);
    lineGpsTmp[0] = lineGpsTmpChild;
    lineGpsMap[gpsKey] = lineGpsTmp;
    if (Object.keys(stationTmp).length) {
      const stationFirstTmp = stationTmp[0];
      let stationEditTmp: any = {};
      for (let index in stationFirstTmp) {
        stationEditTmp[index] = stationFirstTmp[index];
      }
      stationEditTmp.sequence = gpsEditTmp.sequence;
      stationTmp.push(stationEditTmp);
    }
  }
}
